package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.LoginOneEntity;
import com.lanou.lenanapp.view.CircleImage;
import com.lanou.lenanapp.view.CustomDialog;

import java.io.File;
import java.util.List;

/**
 * 账号管理界面
 */
public class AccountActivity extends Activity implements View.OnClickListener {

    private ImageView back_account;
    private RelativeLayout thirdpartyBtn, thisEnter;
    private TextView username, status;
    private DatabaseHelper helper;
    private String phone;
    private CircleImage account_acatar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_account);

        init();

    }

    private void init() {
        helper = DatabaseHelper.getHelper();
        account_acatar = (CircleImage) findViewById(R.id.account_acatar);//头像
        back_account = (ImageView) findViewById(R.id.back_account);
        back_account.setOnClickListener(this);
        thirdpartyBtn = (RelativeLayout) findViewById(R.id.thirdpartyBtn);//新浪微博的行布局
        thirdpartyBtn.setOnClickListener(this);
        thisEnter = (RelativeLayout) findViewById(R.id.thisEnter);//本地登录行布局
        thisEnter.setOnClickListener(this);
        status = (TextView) findViewById(R.id.status);//是否登录状态
        username = (TextView) findViewById(R.id.username);//用户名

        //进入账号页面时 判断单独数据库里是否有账号(即判断是否登录)
        List<LoginOneEntity> list = helper.queryLoginOne();
        if (list != null && list.size() > 0){
            String name = list.get(0).getOnename();
            username.setText(name);
            status.setText("已登录");
            setHeaderIcon();//设置头像
        }else {
            username.setText("昵称");
            status.setText("未登录");
        }

    }

    /**设置头像**/
    private void setHeaderIcon() {
        List<LoginOneEntity> list = helper.queryLoginOne();
        if (list != null && list.size() > 0) {
            String n = list.get(0).getOnephone();
            File file = new File(Environment.getExternalStorageDirectory() + "/" + n + ".jpg");
            if (file.exists()) {
                Bitmap bitmap2 = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + n + ".jpg");
                account_acatar.setImageBitmap(bitmap2);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_account://返回按钮
                finish();
                break;
            case R.id.thirdpartyBtn://第三方登录
                showDialogThirdPartyBtn();
                break;
            case R.id.thisEnter://本地登录
                List<LoginOneEntity> list = helper.queryLoginOne();
                if (list != null && list.size() > 0){
                    showDeleteDialog();//如果已经登录弹出的提示框
                }else{
                    showalertDialog();//如果没登陆弹出的提示框
                }
                break;
        }
    }

    /**
     * 如果已登录账号弹出的提示框
     */
    private void showDeleteDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("您要注销本次登录么?");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                helper.deleteAllLoginOne();//删除当前单独得数据库里的内容
                Intent intent = new Intent(AccountActivity.this, LoginActivity.class);
                startActivity(intent);
                dialog.dismiss();
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 第三方登录弹出框
     * **/
    private void showDialogThirdPartyBtn() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("您确定要使用第三方登录么?");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();//关闭dialog
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 如果没登陆点击登录时的弹出框
     * **/
    private void showalertDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("您确定要使用本地账号登陆么?" + "\n相信我...根本没什么用...");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(AccountActivity.this, LoginActivity.class);
                startActivity(intent);
                dialog.dismiss();//关闭当前弹出框
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭dialog
            }
        });
        builder.create().show();//显示弹出框
    }






}
